﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StableDiffusionTools.Framework
{
    public class FormEntry
    {
        public IForm Form { get; set; }
        public IFormData Data { get; set; }

        public FormEntry(IForm form, IFormData data)
        {
            Form = form;
            Data = data;
        }

        public Form GetForm()
        {
            Form result = null;
            if (Form is Form f)
            {
                if (f.Disposing || f.IsDisposed)
                {
                    f = Activator.CreateInstance(f.GetType()) as Form;
                    Form = f as IForm;
                }
                result = f;
            }
            return result;
        }

        public bool MatchesName(string text)
        {
            return Data.Name.Equals(text);
        }

        private bool IsValid()
        {
            bool result = false;
            if(Form is Form f)
            {
                result = f != null && !f.Disposing && !f.IsDisposed;
            }
            return result;
        }

        public bool IsBusy()
        {
            return IsValid() && Form.Busy;
        }

        public bool Close()
        {
            return Form.CloseFromTaskTray();
        }
    }
}
